package com.traffic.between.adaptor;

import java.util.Date;

import org.apache.log4j.Logger;

import com.traffic.between.annotation.ChooseProvider;
import com.traffic.between.config.BetweenProperties;
import com.traffic.between.config.OrderInfoContext;
import com.traffic.between.redis.running.BsmOrderResultExecutor;
import com.traffic.between.service.CongChongServiceImpl;
import com.traffic.managent.service.order.bean.OrderBean;

import net.sf.json.JSONObject;
@ChooseProvider(channel = "cc")
public class CongChongAdaptor implements IAdaptor{

	private static Logger log = Logger.getLogger(CongChongAdaptor.class);

	
	public void order() {
		
		OrderBean orderBean = OrderInfoContext.getContext();
		orderBean.setSendTime(new Date());
		
		String productId = orderBean.getProductCode();
		String phoneNo = orderBean.getPhoneNo();
		String cstmOrderNo = orderBean.getIds();
		String callBackUrl = BetweenProperties.CALLBACK_URL+ "Congc";
	
		String result = CongChongServiceImpl.service.order(productId, phoneNo, cstmOrderNo,callBackUrl);
		log.info("聪聪订购请求返回的结果：["+result+"],该订单号为："+cstmOrderNo);
		JSONObject json = JSONObject.fromObject(result);
        
		String code = json.getString("code");
		
		if ("0000".equals(code)) {
			String data = json.getString("data");
			JSONObject dataJson = JSONObject.fromObject(data);
			orderBean.setMessage((String) json.get("msg"));
			orderBean.setSuppOrderNo(dataJson.getString("orderNo"));
			orderBean.setErrorMsg("订购中");
			orderBean.setStatus("10");
		} else {

			orderBean.setMessage((String) json.get("msg"));
			orderBean.setErrorMsg((String) json.get("msg"));
			orderBean.setStatus("8");
		}
		BsmOrderResultExecutor.service.setOrder(orderBean);
	}

	public void status() {

		OrderBean orderBean = OrderInfoContext.getContext();

		String cstmOrderNo = orderBean.getSuppOrderNo();
		log.info("聪聪查询请求status的参数："+cstmOrderNo);
		if ("".equals(cstmOrderNo)) {
			orderBean.setMessage("该订单业务流水为空");
			orderBean.setErrorMsg("受理中，无订单");
			orderBean.setStatus("");
		}else {

		String result = CongChongServiceImpl.service.orderStatus(cstmOrderNo);

		JSONObject json = JSONObject.fromObject(result);

		String code = json.getString("code");
		
		if ("0007".equals(code)) {//订购成功
			orderBean.setStatus("7");
			orderBean.setMessage("订购成功");
		    orderBean.setErrorMsg("订购成功");
		}else if ("0008".equals(code)) {//订购失败
			orderBean.setStatus("8");
			orderBean.setMessage(json.getString("msg"));
			orderBean.setErrorMsg(json.getString("msg"));
		}else {//订购中
			orderBean.setStatus("1");
			orderBean.setMessage(json.getString("msg"));
			orderBean.setErrorMsg(json.getString("msg"));
		}
		}
		BsmOrderResultExecutor.service.setOrder(orderBean);
	}
}
